package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.goods.GoodsSpecificationsValues;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsValuesAddParam;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsValuesQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsValuesUpdateParam;
import com.hoshiicloud.goods.service.GoodsSpecificationsValuesService;
import com.hoshiicloud.goods.vo.GoodsSpecificationsValuesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品规格_值 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-specifications-values")
@Api(value = "商品规格_值", tags = {"商品规格_值"})
public class GoodsSpecificationsValuesController {

    @Autowired
    private GoodsSpecificationsValuesService goodsSpecificationsValuesService;


    @ApiOperation(value = "分页查询商品规格_值")
    @PostMapping("/getPage")
    public Response<Page<GoodsSpecificationsValuesVo>> page(@RequestBody GoodsSpecificationsValuesQueryParam param) {
        try {
            Page<GoodsSpecificationsValuesVo> page = new Page<>(Long.parseLong(param.getPageNumber()),
                    Long.parseLong(param.getPageSize()));
            String key = "GoodsSpecificationsValuesQueryParam{" + param.toString() + "},Page{pageNumber=" + param
                    .getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(goodsSpecificationsValuesService.getPage(page, param, key));
        } catch (Exception e) {
            log.error("分页查询商品规格_值异常", e);
            return Response.failed("分页查询商品规格_值异常");
        }
    }

    /**
     * 根据specId查询出规格NameList
     *
     * @param specId specId
     * @return Response<List>
     */
    @GetMapping("/getSpecificationsValueToNameListBySpecId")
    public Response<List<GoodsSpecificationsValues>> getSpecificationsValueToNameListBySpecId(
            @RequestParam("specId") Long specId) {
        return Response.data(goodsSpecificationsValuesService.getSpecificationsValueToNameListBySpecId(specId));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品规格_值")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {

        return Response.data(goodsSpecificationsValuesService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品规格_值")
    public Response<Boolean> save(@RequestBody GoodsSpecificationsValuesAddParam param) {
        // Bean数据转换
        GoodsSpecificationsValues entity;
        try {
            entity = BeanUtils.covertType(param, GoodsSpecificationsValues.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsSpecificationsValuesService.save(entity));
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改商品规格_值")
    public Response<Boolean> update(@RequestBody GoodsSpecificationsValuesUpdateParam param) {
        // Bean数据转换
        GoodsSpecificationsValues entity;
        try {
            entity = BeanUtils.covertType(param, GoodsSpecificationsValues.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsSpecificationsValuesService.updateById(entity));
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品规格_值")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return Response.data(goodsSpecificationsValuesService.deleteById(id));
    }

    @ApiOperation(value = "根据id集合查询商品规格_值")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(goodsSpecificationsValuesService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
